/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.nutch.analysis.zh;

// JDK imports
import java.io.Reader;

// StringReader for testing
import java.io.StringReader;

// Lucene imports
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.Token;

// Nutch imports
import org.apache.nutch.analysis.NutchAnalyzer;

// Logger
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; 

/**
 * A simple Chinese Analyzer that wraps the Lucene one.
 * @author
 */
public class ChineseAnalyzer extends NutchAnalyzer {
    
    private final static Analyzer ANALYZER = 
            new org.apache.lucene.analysis.cn.ChineseAnalyzer();
    
    // private final static Analyzer ANALYZER = 
    //        new org.apache.lucene.analysis.cjk.CJKAnalyzer();

    private final static Log LOG = LogFactory.getLog(ChineseAnalyzer.class); 

    /** Creates a new instance of ChineseAnalyzer */
    public ChineseAnalyzer() { }


    public TokenStream tokenStream(String fieldName, Reader reader) {
        LOG.info("Chinese Analyzer");
        
        TokenStream tokens = ANALYZER.tokenStream(fieldName, reader);
        Token token;
        try {
            while ((token = tokens.next()) != null) {
                System.out.println("Chinese Analyzer token is " + token.toString());
            }
            reader.reset();
        }
        catch (Exception e) {
            System.err.println("error in Chinese Analyzer");
        } 
        return ANALYZER.tokenStream(fieldName, reader);
    }


    // this suppose to work with 
    // bin/nutch plugin analysis-zh org.apache.nutch.analysis.zh.ChineseAnalyzer
    public static void main(String args[]) {
        Reader reader = new StringReader(args[0]);
        String fieldName = "test";
        TokenStream tokens = ChineseAnalyzer.ANALYZER.tokenStream(fieldName, reader);
        Token token;
        try {
                System.out.println("Chinese Analyzer");
                while ((token = tokens.next()) != null) {
                    System.out.println("Chinese Analyzer token is " + token.toString());
                }
        }
        catch (Exception e) {
            System.err.println("error in Chinese Analyzer");
        } 
    }
}
